import os
from tools.funciton import read_json_file, write_xlsx, timestamp_switch_date, replace_column_values
from config.configs import DATA_PATH, DESKTOP_PATH, DOWNLOAD_PATH

file = os.path.join(DESKTOP_PATH, 'balancerecords.json')


def balancrecords_export_xlsx(file_path):
    """快递小程序线上余额变更记录导出"""
    li = read_json_file(file)
    li1 = []
    for i in li:
        if 'orderId' not in i:
            i['orderId'] = ''
        if 'expressNo' not in i:
            i['expressNo'] = ''
        if 'operateNumber' not in i:
            i['operateNumber'] = ''
        if 'operateNumbers' not in i:
            i['operateNumbers'] = ''
        if i['operateNumbers'] is []:
            i['operateNumbers'] = ''
        if type(i['operateNumbers']) is list and len(i['operateNumbers']) == 1:
            i['operateNumbers'] = i['operateNumbers'][0]
        if type(i['operateNumbers']) is list and len(i['operateNumbers']) > 1:
            res = i['operateNumbers']
            i['operateNumbers'] = res[0]
            i['operateNumbers1'] = res[1]
        i['createdAt'] = i['createdAt']['$numberLong']
        i['updatedAt'] = i['updatedAt']['$numberLong']
    for z in li:
        if 'operateNumbers1' in z:
            li1.append([z['_id'], z['userId'], z['orderId'], z['expressNo'], z['operateNumber'], z['type'], z['cause'],
                        z['channel'],
                        z['before'], z['amount'], z['after'], timestamp_switch_date(int(z['createdAt'])),
                        timestamp_switch_date(int(z['updatedAt'])), z['operateNumbers'], z['operateNumbers1']])
        else:
            li1.append([z['_id'], z['userId'], z['orderId'], z['expressNo'], z['operateNumber'], z['type'], z['cause'],
                        z['channel'],
                        z['before'], z['amount'], z['after'], timestamp_switch_date(int(z['createdAt'])),
                        timestamp_switch_date(int(z['updatedAt'])), z['operateNumbers'],
                        ''])



balancrecords_export_xlsx(file)